package cn.org.timespace.plat.dao.mc.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.plat.action.mc.PlatMconfModel;
import cn.org.timespace.plat.dao.mc.PlatMconfDao;
import cn.org.timespace.plat.vo.mc.PlatMconf;

public class PlatMconfDaoImpl  extends TSBaseDAO implements PlatMconfDao{
	@Override
	public PlatMconf findById(String id) {
			// TODO Auto-generated method stub
			log.debug("getting PlatMconf instance with id: " +id);
			try{
				PlatMconf instance = (PlatMconf)getHibernateTemplate().get(
						"cn.org.timespace.plat.vo.mc.PlatMconf", id);
				return instance;
			}catch(RuntimeException re){
				log.debug("get fail",re);
				throw re;
		}
	}
	
	@Override
	public void save(PlatMconf instance) {
	
		log.debug("saving PlatMconf Instance");
		try{
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("saving successful");
		}catch(RuntimeException re){
			log.debug("saving fail",re);
			throw re;
		}
	}

	@Override
	public void delete(PlatMconf instance) {
		log.debug("deleting PlatMconf Instance");
		try{
			getHibernateTemplate().delete(instance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public void update(PlatMconf instance) {
		
		log.debug("update PlatMconf Instance");
		try{
			getHibernateTemplate().update(instance);
			
		}catch(RuntimeException re){
			log.error("update failed!",re);
			throw re;
		}	
	}

	@Override
	public List<PlatMconf> findByCondition(PlatMconfModel model) {
		log.debug("findByCondition PlatMconf Instance");
		StringBuffer queryString = new StringBuffer("select m from PlatMconf mc,PlatMcInfo mi where mc.mcInfoId = mi.theId ");
		if(!StringUtils.isEmpty(model.getTheId())){
			queryString.append("and m.mcid = '" + model.getTheId() + "'" );
		}
		if(!StringUtils.isEmpty(model.getMcInfoId())){
			queryString.append("and m.mcid = '" + model.getMcInfoId() + "'" );
		}
		queryString.append(" order by m.mcid desc");
		return getHibernateTemplate().find(queryString.toString());
	}
	
	@Override
	public PlatMconf findByMcInfoId(String mcInfoId){
		log.debug("findByCondition PlatMconf Instance");
		StringBuffer queryString = new StringBuffer("from PlatMconf mc where mc.mcInfoId = '"+mcInfoId +"'");
		
		queryString.append(" order by mc.mcInfoId desc");
		List<PlatMconf> mcList = getHibernateTemplate().find(queryString.toString());
		if(mcList != null && mcList.size()>0){
			return mcList.get(0);
		}else{
			return null;
		}
		
	}

	@Override
	public List<Object> findByCondition(PlatMconfModel model, Pager p) {
	    log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from PlatMconf m where 1=1");
//			if(!StringUtils.isEmpty(model.getMcId())){
//				queryString.append("and m.mcid = '" + model.getMcId() + "'" );
//			}
//			if(!StringUtils.isEmpty(model.getStatus())){
//				queryString.append("and m.mcid = '" + model.getStatus() + "'" );
//			}
			queryString.append(" order by m.mcid desc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
	
	@Override
	public PlatMconf findByMcId(String mcId){
		log.debug("findByCondition PlatMconf Instance");
		StringBuffer queryString = new StringBuffer("select mc from PlatMconf mc,PlatMcInfo mi where mc.mcInfoId = mi.theId and mi.mcId = '"+mcId +"'");
		queryString.append(" order by mc.mcInfoId desc");
		List<PlatMconf> mcList = getHibernateTemplate().find(queryString.toString());
		if(mcList != null && mcList.size()>0){
			return mcList.get(0);
		}else{
			return null;
		}
		
	}
	
}

